package com.example.it.looker.view;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by 鲁迅认识的那只猹 on 9/5/2017 11:11 AM.
 * Emial 1258730808@qq.com
 * Desc:
 */

public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter {

    //页首Item的标识
    public static final int HEADER = 1;
    //页脚Item的标识
    public static final int FOOTER = 2;
    //普通Item的标识
    public static final int NORMAL = 3;


    private Context context;
    private List<T> dataSource;
    private int resId;

    //页眉的视图
    private View headerView;
    //页脚的视图
    private View footerView;

    /**
     * 构造函数
     *
     * @param context    上下文对象
     * @param dataSource 所需数据
     * @param resId      Item的布局资源Id
     */
    public BaseRecyclerAdapter(Context context, List<T> dataSource, int resId) {
        this.context = context;
        this.dataSource = dataSource;
        this.resId = resId;
    }

    /**
     * 抽象方法，我们需要在BaseRecyclerAdapter子类中自己实现它，初始化，ViewHolder，的方法，我们需要在此方法中，返回，我们想要的Holder
     */
    @Override
    public abstract RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType);

    /**
     * 抽象方法，我们需要在BaseRecyclerAdapter子类中自己实现它，来进行赋值操作
     */
    @Override
    public abstract void onBindViewHolder(RecyclerView.ViewHolder holder, int position);

    /**
     * 返回Item的数量，如果有页眉，页脚，那么也需要被计算在其中
     */
    @Override
    public int getItemCount() {
        int count = dataSource.size();
        //判断是否，存在页眉页脚
        if (headerView != null)
            count += 1;
        if (footerView != null)
            count += 1;
        return count;
    }

    /**
     * 获取在指定位置的，的Item的类型
     */
    @Override
    public int getItemViewType(int position) {
        if (headerView != null && position == 0)
            return HEADER;
        else if (footerView != null && position == getItemCount() - 1)
            return FOOTER;
        else
            return NORMAL;
    }

    /**
     * 当Adapter和RecyclerView结合在一起的时候调用
     */
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        //获取RecyclerView的布局Manager
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        //判断是否是GridLayoutMnaager
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
            //设置一个Item所占的格数
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    //若果是页眉页脚那么沾满一行，如果是普通条目，占一格
                    return (isHeader(position) || isFooter(position)) ? gridLayoutManager.getSpanCount() : 1;
                }
            });
        }
    }

    /**
     * 当View被添加进当窗体后调用
     */
    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        //如果当前的View是Header或者Footer才进行处理
        if (isHeader(holder.getLayoutPosition()) || isFooter(holder.getLayoutPosition())) {
            //判断View是否使用了 StaggeredGridLayoutManager.LayoutParams 的布局参数
            if (holder.itemView.getLayoutParams() instanceof StaggeredGridLayoutManager.LayoutParams) {

                StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
                //当为ture 时候，那么当前View沾满整个行
                params.setFullSpan(true);
            }
        }
    }

    /**
     * 获取真实的位置,如果存再页首，onBindViewHolder方法提供的Position就会比正常的大1
     */
    public int getActualPosition(int position) {
        if (footerView != null)
            return position - 1;
        return position;
    }

    /**
     * 判断指定的位置是否是页眉
     */
    public boolean isHeader(int postion) {
        return postion == 0 && headerView != null;
    }

    /**
     * 判断指定的位置是否是页脚
     */
    public boolean isFooter(int postion) {
        return postion == this.getItemCount() - 1 && footerView != null;
    }

    public View getFooterView() {
        return footerView;
    }

    public void setFooterView(View footerView) {
        this.footerView = footerView;
    }

    public View getHeaderView() {
        return headerView;
    }

    public void setHeaderView(View headerView) {
        this.headerView = headerView;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public List<T> getDataSource() {
        return dataSource;
    }

    public void setDataSource(List<T> dataSource) {
        this.dataSource = dataSource;
    }

    public int getResId() {
        return resId;
    }

    public void setResId(int resId) {
        this.resId = resId;
    }

    /**
     * ViewHolder，如果是Header或者Footer那么不进行任何处理，直接返回
     */
    public class RecyclerViewHolder extends RecyclerView.ViewHolder {
        public RecyclerViewHolder(View itemView) {
            super(itemView);
            if (headerView != null && itemView == footerView)
                return;
            if (footerView != null && itemView == headerView)
                return;
        }
    }
}
